package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/10/19 11:57
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;
    /**
     * 条件分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public List<Category> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        List<Category> categories = categoryMapper.pageQuery(categoryPageQueryDTO);
        return categories;
    }

    /**
     * 新增菜品/套餐分类
     * @param categoryDTO
     */
    @Override
    public void saveCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(0);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.save(category);
    }

    /**
     * 根据分类类型查询
     * @param type
     * @return
     */
    @Override
    public List<Category> queryByCategory(Integer type,String name) {
        List<Category> categorys = categoryMapper.queryByCategory(type,name);
        return categorys;
    }

    /**
     * 修改分类信息
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category); // 将dto转换为JavaBean
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    /**
     * 修改分类启用还是禁用
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Integer id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id.longValue());
//        category.setUpdateUser(BaseContext.getCurrentId());
//        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    public void deleteCategory(Long id) {
        // 删除分类前判断该分类下是否还有菜品/套餐
        Integer dishCount = dishMapper.isExistDish(id);
        Integer setmealCount = setmealMapper.isExistSetmeal(id);
        if(dishCount > 0){  // 该分类下还有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        } else if (setmealCount > 0) {  // 该分类下还有套餐，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }else { // 允许删除
            categoryMapper.delete(id);
        }
    }

    /**
     * 增加菜品时，根据类型查询所有菜品/套餐
     * @param type
     * @return
     */
    @Override
    public List<Category> saveDishByCategory(Integer type) {
       return categoryMapper.saveDishByCategory(type);
    }

    /**
     * 用户端显示分类信息
     * @param type
     * @return
     */
    @Override
    public List<Category> queryDishOrSetmealByCategory(Integer type) {
        List<Category> list = categoryMapper.queryDishOrSetmealByCategory(type);
        return list;
    }
}
